//
//  MyTools.m
//  Music
//
//  Created by dllo on 16/6/6.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MyTools.h"
#import <AFNetworking.h>
@implementation MyTools
+(void)getNetWithUrl:(NSString *)url body:(id)body header:(NSDictionary *)headers response:(LHQResponseStyle)response success:(blockOfSuccess)success failure:(blockOfFailure)failure
{
    //1.获取网络管理者
    AFHTTPSessionManager  *manager = [ AFHTTPSessionManager    manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //2.请求头设置
    if(headers){
        for (NSString  *key in headers.allKeys) {
            [manager.requestSerializer  setValue:headers[key] forHTTPHeaderField:key];
        }
    }
    
    //3.设置返回数据的类型
    switch (response) {
        case LHQJSON:
            manager.responseSerializer = [AFJSONResponseSerializer  serializer];
            break;
        case LHQXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case LHQDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        default:
            break;
    }
    //请求https
    manager.securityPolicy.allowInvalidCertificates = YES;
    //4设置响应数据类型
    //ios9
    url = [url  stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet   URLQueryAllowedCharacterSet]];
    
    //设置相应数据类型
    [manager.responseSerializer  setAcceptableContentTypes:[NSSet  setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    
    //发送请求
    [manager  GET:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        success(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
        NSLog(@"%@",error);
    }];
    
}


+(void)postNetWithUrl:(NSString *)url body:(id)body bodyStyle:(NSRequsetStyle)bodyStyle header:(NSDictionary *)headers response:(LHQResponseStyle)response success:(blockOfSuccess)success failure:(blockOfFailure)failure
{
    
    //1.获取网络管理者
    AFHTTPSessionManager  *manager = [ AFHTTPSessionManager    manager];
    
    //2.设置Body数据类型
    
    switch (bodyStyle) {
        case LHQBodyJSON:
            manager.responseSerializer = [AFJSONResponseSerializer  serializer];
            break;
        case LHQBodyString:
            [manager.requestSerializer  setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable * _Nullable error) {
                return  parameters;
            }];
            break;
        default:
            break;
    }
    //请求https
    manager.securityPolicy.allowInvalidCertificates = YES;
    
    
    //3.请求头设置
    if(headers){
        for (NSString  *key in headers.allKeys) {
            [manager.requestSerializer  setValue:headers[key] forHTTPHeaderField:key];
        }
    }
    
    //4.设置返回数据的类型
    switch (response) {
        case LHQJSON:
            manager.responseSerializer = [AFJSONResponseSerializer  serializer];
            break;
        case LHQXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case LHQDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        default:
            break;
    }
    //5.设置响应数据类型
    //ios9
    url = [url  stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet   URLQueryAllowedCharacterSet]];
    
    //6.设置相应数据类型
    [manager.responseSerializer  setAcceptableContentTypes:[NSSet  setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    
    //7.发送请求
    [manager  POST:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        success(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
        NSLog(@"%@",error);
    }];
    
    
    
    
}
@end
